<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	protected $_layoutFile = '';
	
	protected $_layout;
	
	protected $_titles = array();
	
	public function __construct() {
		parent::__construct();
		$this->load->library('app');
		$this->_layoutFile = APPPATH . 'views/layout.xml';
		$this->_layout = App::objectSingleton('Core_Layout');
		App::registry('controller', $this);
	}

	public function loadLayout() {
		$xmlStr = file_get_contents($this->_layoutFile);
		$this->_layout->addLayout($xmlStr);
		$this->_layout->addHandle($this->getControllerHandle());
		$this->_layout->loadLayout();
	}
	
	public function renderLayout() {
		$title = implode(' / ', $this->_titles);
		$head = $this->_layout->getBlock('head');
		if ($head) $head->setTitle($title);
		$this->_layout->render();
	}
	
	public function getLayout() {
		return $this->_layout;
	}
	
	public function getControllerHandle() {
		return $this->router->fetch_class().'_'.$this->router->fetch_method();
	}

	protected function _addTitle($s) {
		array_unshift($this->_titles, $s);
	}
	
}
